package com.three.service.impl;


import com.baor.baseUtil.R;
import com.three.dao.StudentDao;
import com.three.domain.Students;
import com.three.service.StudentService;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDao studentDao;

    @Override
    public R updateStudent(Students students) {
        String plainPassword = students.getPassword();
        String hashedPassword = BCrypt.hashpw(plainPassword, BCrypt.gensalt());
        students.setPassword(hashedPassword);
        studentDao.updUser(students);
        return R.ok(studentDao.updateStudent(students));
    }
    //修改信息





    //查询在习生列表
    @Override
    public R studentsList(Integer pageIndex, Integer pageSize,Integer xiaoshou_id) {
        pageIndex = (pageIndex - 1) * pageSize;
        double pageCountstudents =studentDao.PageCountStudents(xiaoshou_id);
        pageCountstudents = Math.ceil(pageCountstudents / pageSize);
        List<Students> studentsList = studentDao.studentsList(pageIndex, pageSize,xiaoshou_id);
        Map<String, Object> map = new HashMap<>();
        map.put("pageCountstudents", pageCountstudents);
        map.put("studentsList", studentsList);
        return R.ok(map);
    }
    //添加在习生
    @Override
    public R addstu(Map<String, Object> map) {
        String name = (String)map.get("name");
        String gender = (String) map.get("gender");
        String class_name = (String)map.get("class_name");
        String graduation_date = (String)map.get("graduation_date");
        String phone = (String)map.get("phone");
        String emergency_name = (String)map.get("emergency_name");
        String emergency_contact = (String)map.get("emergency_contact");
        String home = (String)map.get("home");
        Integer user_id = (Integer)map.get("user_id");
        String school = (String)map.get("school");
        System.out.println(name);
        int row = this.studentDao.addstudent(name, gender, class_name, graduation_date, phone, emergency_name, emergency_contact, home,user_id,school);
        studentDao.addstu(name,gender,phone);
        studentDao.updateUser_id(phone);
        if(row > 0) return R.ok();
        else return R.er();
    }
    //    修改更新在习生信息
    @Override
    public R updateStudents(Students student) {
        studentDao.updateUser(student);
        return R.ok(studentDao.updateStudents(student));
    }
    //软删除在习生
    @Override
    public int softDeleteStudent(int student_id,int user_id) {
        studentDao.softDeleteStudentByuser(user_id);
        return studentDao.softDeleteStudent(student_id);
    }
    //    关键字查询在习生列表
    @Override
    public  R findStudentByname(Integer pageIndex, Integer pageSize,String name,Integer xiaoshou_id) {
        pageIndex = (pageIndex - 1) * pageSize;
        double pageCountstukey =studentDao.PageCountstukey(pageIndex, pageSize,name,xiaoshou_id);
        pageCountstukey = Math.ceil(pageCountstukey / pageSize);
        List<Students> stukeylist= studentDao.getStuByName(pageIndex, pageSize,name,xiaoshou_id);
        Map<String, Object> map = new HashMap<>();
        map.put("pageCountstukey", pageCountstukey);
        map.put("stukeylist", stukeylist);
        return R.ok(map);
    }

    @Override
    public R xiaoshouList() {
        return R.ok(studentDao.xiaoshouList());
    }

    //添加未报名学生
    @Override
    public R addstu_no_sign(Map<String, Object> map) {
        String name = (String)map.get("name");
        String school = (String)map.get("school");
        String phone = (String)map.get("phone");
        Integer xiaoshou_id  = (Integer)map.get("user_id");
        return R.ok(studentDao.addstu_no_sign(name,school,phone,xiaoshou_id));
    }
    //未报名学生列表
    @Override
    public R getstu_no_sign(Integer pageIndex, Integer pageSize, String start_date, String end_date, String keyword, Integer xiaoshou_id) {
        pageIndex = (pageIndex - 1) * pageSize;
        double pageCoun_no_sign =studentDao.pageCoun_no_sign(pageIndex, pageSize,start_date,end_date,keyword,xiaoshou_id);
        pageCoun_no_sign = Math.ceil(pageCoun_no_sign / pageSize);
        List<Students> stukeylist= studentDao.get_no_sign(pageIndex, pageSize,start_date,end_date,keyword,xiaoshou_id);
        Map<String, Object> map = new HashMap<>();
        map.put("pageCoun_no_sign", pageCoun_no_sign);
        map.put("stukeylist", stukeylist);
        return R.ok(map);
    }
    // 批量删除
    @Override
    public R delete_no_sign(List<Integer> student_id) {
        return R.ok(studentDao.delete_no_sign(student_id));
    }
    //批量转移未报到学生
    @Override
    public R update_no_sign(List<Integer> student_id, int xiaoshou_id) {
        return R.ok(studentDao.update_no_sign(student_id,xiaoshou_id));
    }

    @Override
    public R updateStatus(List<Students> students) {
        for (Students student:students) {
            studentDao.updateStatus(student);
        }
        return R.ok();
    }

    @Override
    public R sale_statistics(Map<String, Object> map) {
        String start_date = (String) map.get("start_date");
        String end_date = (String)map.get("end_date");
        Integer xiaoshou_id = (Integer)map.get("xiaoshou_id");


        return R.ok(studentDao.sale_statistics(start_date,end_date,xiaoshou_id));
    }

    @Override
    public R sale_statistics_by_xiaoshou(Map<String, Object> map) {
        Integer xiaoshou_id = (Integer)map.get("xiaoshou_id");
        String date = (String)map.get("date");
        Integer pageIndex = (Integer)map.get("pageIndex");
        Integer pageSize = (Integer)map.get("pageSize");
        pageIndex = (pageIndex - 1) * pageSize;
        double pageCount_statistics =studentDao.pageCount_statistics(date,xiaoshou_id);
        pageCount_statistics = Math.ceil(pageCount_statistics / pageSize);
        List<Students> sale_statistics_by_xiaoshou= studentDao.sale_statistics_by_xiaoshou(date,xiaoshou_id,pageIndex, pageSize);
        Map<String, Object> maps = new HashMap<>();
        maps.put("pageCount_statistics", pageCount_statistics);
        maps.put("sale_statistics_by_xiaoshou", sale_statistics_by_xiaoshou);
        return R.ok(maps);
    }


}
